package schema

import (
	"github.com/shopspring/decimal"
	"lianzi_gos/internal/config"
	"time"

	"lianzi_gos/pkg/util"
)

// 商品表
type Goods struct {
	ID           string          `json:"id" gorm:"size:20;primarykey;"`                         // Unique ID
	UID          string          `json:"uid" gorm:"size:20;index;"`                             //  发布人ID
	WID          string          `json:"w_id" gorm:"size:20;index;"`                            //  归属人ID
	ZUID         string          `json:"z_uid" gorm:"size:20;index;default:'';comment:'指派人ID'"` //  指派人ID
	BusID        string          `json:"bus_id" gorm:"size:20;index;"`                          // 业态
	Username     string          `json:"username" gorm:"size:50;index;default:'';comment:'用户'"`
	TwoImage     string          `json:"two_image" gorm:"size:199;index;default:'';comment:'全景照'"`
	TwoName      string          `json:"two_name" gorm:"size:50;index;default:'';comment:'全套设备'"`
	Nickname     string          `json:"nickname" gorm:"size:150;index;default:'';comment:'别名'"`
	Code         string          `json:"code" gorm:"size:20;index;default:'';comment:'设备编号'"`
	AttrIds      string          `json:"attr_ids" gorm:"size:190;index;default:'';comment:'子商品名称'"`
	MName        string          `json:"m_name" gorm:"size:50;index;default:'';comment:'成交用户'"`
	MPhone       string          `json:"m_phone" gorm:"size:50;index;default:'';comment:'成交手机号'"`
	PickUp       int             `json:"pick_up" gorm:"index;default:1;comment:'1 自提 2 包送'"`
	Phone        string          `json:"phone" gorm:"size:20;index;default:'';comment:'电话'"`
	Price        decimal.Decimal `json:"price" gorm:"type:decimal(10,2);not null;comment:'估价';default:0"`
	TruePrice    decimal.Decimal `json:"true_price" gorm:"type:decimal(10,2);not null;comment:'实际成交价格';default:0"`
	Province     string          `json:"province" gorm:"size:20;index;default:'';comment:'省'"`
	ProvinceId   int64           `json:"province_id" gorm:"index;default:0;comment:'省 id'"`
	CityId       int64           `json:"city_id" gorm:"index;default:0;comment:'市 id'"`
	DistrictId   int64           `json:"district_id" gorm:"index;default:0;comment:'区 id'"`
	City         string          `json:"city" gorm:"size:20;index;default:'';comment:'市'"`
	District     string          `json:"district" gorm:"size:20;index;default:'';comment:'区'"`
	Address      string          `json:"address" gorm:"size:150;index;default:'';comment:'地址'"`
	Contract     string          `json:"contract" gorm:"size:150;index;default:'';comment:'合同'"`
	ContractDesc string          `json:"contract_desc" gorm:"size:150;index;default:'';comment:'备注'"`
	Speed        int64           `json:"speed" gorm:"size:2;index;default:0;comment:'-1 驳回  1发布 2 指派  3审核  4 完成 '"` //高
	Recommend    int64           `json:"recommend" gorm:"size:2;index;default:1;comment:'1 正常  2 推荐首页 '"`           //推荐
	Sale         int64           `json:"sale" gorm:"size:2;index;default:1;comment:'1 正常  2 急售 '"`                  //推荐
	Selection    int64           `json:"selection" gorm:"size:2;index;default:1;comment:'1 正常 2 精选 '"`              //推荐
	OverruleMsg  string          `json:"overrule_msg" gorm:"size:200;index;default:'';comment:'驳回 信息'"`             //高
	Types        string          `json:"types" gorm:"size:10;index;default:'';comment:'one 单卖  two 多卖';"`
	Description  string          `json:"description" gorm:"size:255"` // Details about dictionary
	Status       string          `json:"status" gorm:"size:20;index"` // Status of dictionary (disabled, enabled)
	CreatedAt    time.Time       `json:"created_at" gorm:"index;"`    // Create time
	UpdatedAt    time.Time       `json:"updated_at" gorm:"index;"`    // Update time
	SuccessAt    time.Time       `json:"success_at" gorm:"index;"`    // 完成合同 time
	OverruleAt   time.Time       `json:"overrule_at" gorm:"index;"`   // 驳回time
	ApprovedAt   time.Time       `json:"approved_at" gorm:"index;"`   // 审核time
	PublishAt    time.Time       `json:"publish_at" gorm:"index;"`    // 发布time
	ZhiAt        time.Time       `json:"zhi_at" gorm:"index;"`        // 审核time
	GoodsAttr    `json:"goods_attr" gorm:"-"`
	AttrArr      string `json:"attr_arr" gorm:"-"`
	IsCollect    bool   `json:"is_collect" gorm:"-"`
}
type GoodsArr struct {
	//WID         string          `json:"wid"` //  归属人ID
	PName       string          `json:"p_name"`
	Username    string          `json:"username"`
	TwoName     string          `json:"two_name"`
	TwoImage    string          `json:"two_image"`
	Nickname    string          `json:"nickname"`
	Phone       string          `json:"phone"`
	Spec        string          `json:"spec"`
	PPhone      string          `json:"p_phone"`
	Price       decimal.Decimal `json:"price"`
	TotalPrice  decimal.Decimal `json:"total_price"`
	Unit        decimal.Decimal `json:"unit"`
	Province    string          `json:"province"`
	City        string          `json:"city"`
	AttrId      string          `json:"attr_id"`
	Cid         string          `json:"cid"`
	Types       string          `json:"types"`
	Fineness    int64           `json:"fineness"`
	District    string          `json:"district"`
	Speed       int64           `json:"speed"`
	Image       string          `json:"image"`
	PriceImage  string          `json:"price_image"`
	ID          string          `json:"id"`
	Name        string          `json:"name"`
	Year        int64           `json:"year"`
	OverruleMsg string          `json:"overrule_msg"`
	Address     string          `json:"address"`
	Code        string          `json:"code"`
	PAvatar     string          `json:"p_avatar"`
	OverruleAt  time.Time       `json:"overrule_at"`
	PublishAt   time.Time       `json:"publish_at"`
	CreatedAt   time.Time       `json:"created_at"`
	IsAuth      bool            `json:"is_auth" gorm:"-"`
	Selection   int64           `json:"selection"`
	Method      int64           `json:"method"`
	Sale        int64           `json:"sale"`
	Recommend   int64           `json:"recommend"`
	Size        string          `json:"size"`
	Power       string          `json:"power"`
	Diameter    string          `json:"diameter"`
	Capacity    string          `json:"capacity"`
	CateName    string          `json:"cate_name"`
	BusName     string          `json:"bus_name"`
	BName       string          `json:"b_name"`
	Stock       int64           `json:"stock"`
	VideoUrl    string          `json:"video_url"`
	Desc        string          `json:"desc"`
}
type GoodsAdmin struct {
	IsCollect bool `json:"is_collect"`
	GoodsArr
	//Children []*GoodsAttr `json:"childrens"  gorm:"-"`
}

// 管理后台的 struct
type GoodsAdminS struct {
	Goods
	PName    string       `json:"p_name"`
	PPhone   string       `json:"p_phone"`
	PAvatar  string       `json:"p_avatar"`
	Children []*GoodsAttr `json:"childrens"  gorm:"-"`
}
type GoodsBelonging struct {
	Goods
	Children []*GoodsAttr `json:"childrens"  gorm:"-"`
}

func (a *Goods) TableName() string {
	return config.C.FormatTableName("goods")
}

// Defining the query parameters for the `Goods` struct.
type GoodsQueryParam struct {
	Price      int64  `form:"price"`
	Code       string `form:"code"`
	Name       string `form:"name"`
	TwoName    string `form:"two_name"`
	Types      string `form:"types"`
	Year       int64  `form:"year"`
	Recommend  int64  `form:"recommend"`
	Source     int64  `form:"source"`
	CityId     int64  `form:"city_id"`
	StoreType  string `form:"store_type"`
	Speed      int64  `form:"speed"`
	PriceOrder string `form:"price_order"`
	UID        string `form:"uid"`
	BusId      string `form:"bus_id"`
	Cid        string `form:"cid"`
	Status     string `form:"status"`
	util.PaginationParam
}

// Defining the query options for the `Goods` struct.
type GoodsQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Goods` struct.
type GoodsQueryResult struct {
	Data       Goodss
	PageResult *util.PaginationResult
}

// Defining the query result for the `Goods` struct.
type GoodsQueryResultAdmin struct {
	Data       GoodsAdminWeb
	PageResult *util.PaginationResult
}

// 归属人 数据
type GoodsQueryResultBelonging struct {
	Data       GoodsBelongingDetail
	PageResult *util.PaginationResult
}
type Goodss []*GoodsArr
type GoodsAdmins []*GoodsAdmin
type GoodsAdminWeb []*GoodsAdminS // 管理后台
type GoodsDetail GoodsAdmin
type GoodsBelongingDetail GoodsBelonging

// Defining the data structure for creating a `Goods` struct.
type GoodsForm struct {
	Username    string          `json:"username" binding:"required,max=32"`     // 店铺名
	Phone       string          `json:"phone" binding:"required,max=20"`        // 手机号
	Price       decimal.Decimal `json:"price"`                                  //  价格
	Code        string          `json:"code" binding:"required,max=20"`         //设备编号
	Province    string          `json:"province" binding:"required,max=20"`     //省
	TwoName     string          `json:"two_name"`                               // 全套设备名称
	TwoImage    string          `json:"two_image"`                              //  全景照
	City        string          `json:"city" binding:"required,max=20"`         //市
	District    string          `json:"district" binding:"required,max=60"`     //区
	BusID       string          `json:"bus_id" binding:"required,max=60"`       // 业态
	Address     string          `json:"address" binding:"required,max=60"`      // 地址
	ProvinceId  int64           `json:"province_id" binding:"required"`         //省ID
	CityId      int64           `json:"city_id" binding:"required"`             // 市ID
	DistrictId  int64           `json:"district_id" binding:"required"`         //区ID
	Description string          `json:"description"`                            // 其他描述
	AttrIds     string          `json:"attr_ids" binding:"required"`            // 其他描述
	Types       string          `json:"types" binding:"required,oneof=one two"` // one 单卖  two 多卖
}

/*
*
提交合同
完成步骤
*/
type GoodsContract struct {
	Contract     string `json:"contract" binding:"required,max=120"` //合同照片
	MName        string `json:"m_name" binding:"required"`           //高
	MPhone       string `json:"m_phone" binding:"required"`          //高
	TruePrice    string `json:"true_price" binding:"required"`       //高
	ContractDesc string `json:"contract_desc"`
}

/*
*
修改 two
*/
type GoodsTwoForm struct {
	Username    string `json:"username" binding:"required,max=32"`     // 店铺名
	Phone       string `json:"phone" binding:"required,max=20"`        // 手机号
	Province    string `json:"province" binding:"required,max=20"`     //省
	TwoName     string `json:"two_name"`                               // 全套设备名称
	TwoImage    string `json:"two_image"`                              //  全景照
	City        string `json:"city" binding:"required,max=20"`         //市
	District    string `json:"district" binding:"required,max=60"`     //区
	BusID       string `json:"bus_id" binding:"required,max=60"`       // 业态
	Address     string `json:"address" binding:"required,max=60"`      // 地址
	ProvinceId  int64  `json:"province_id" binding:"required"`         //省ID
	CityId      int64  `json:"city_id" binding:"required"`             // 市ID
	DistrictId  int64  `json:"district_id" binding:"required"`         //区ID
	Description string `json:"description"`                            // 其他描述
	Types       string `json:"types" binding:"required,oneof=one two"` // one 单卖  two 多卖
}

/*
*推荐
 */
type GoodsRecommend struct {
	Recommend int64 `json:"recommend" binding:"required"` //合同照片
}

/*
*推荐
 */
type GoodsSale struct {
	Sale int64 `json:"sale" binding:"required"` //合同照片
}

/*
*推荐
 */
type GoodsSelection struct {
	Selection int64 `json:"selection" binding:"required"` //合同照片
}

/*
* 指派员工
 */
type GoodsZhipai struct {
	WID string `json:"w_id" binding:"required"` // 指派员工的uid
}

/*
*
驳回操作
*/
type GoodsOverrule struct {
	OverruleMsg string `json:"overrule_msg" binding:"required,max=120"` //驳回
}
type GoodsPrice struct {
	Price decimal.Decimal `json:"price" binding:"required"` //驳回
}
type GoodsStatus struct {
	Status string `json:"status" binding:"required"` //驳回
}

// A validation function for the `GoodsForm` struct.
func (a *GoodsForm) Validate() error {
	return nil
}

/*
完成订单操作
*/
func (a *GoodsContract) FillToContract(goods *Goods) error {
	decimalValue, _ := decimal.NewFromString(a.TruePrice)
	goods.Contract = a.Contract
	goods.MName = a.MName
	goods.TruePrice = decimalValue
	goods.MPhone = a.MPhone
	goods.ContractDesc = a.ContractDesc
	return nil
} /*
完成订单操作
*/
func (a *GoodsTwoForm) FillToTwoSuccess(goods *Goods) error {
	goods.Username = a.Username
	goods.Phone = a.Phone
	goods.Province = a.Province
	goods.ProvinceId = a.ProvinceId
	goods.City = a.City
	goods.CityId = a.CityId
	goods.District = a.District
	goods.DistrictId = a.DistrictId
	goods.BusID = a.BusID
	goods.Address = a.Address
	goods.Types = a.Types
	goods.TwoImage = a.TwoImage
	goods.TwoName = a.TwoName
	return nil
}

/*
完成订单操作 UpdateOverrule
*/
func (a *GoodsOverrule) FillToOverrule(goods *Goods) error {
	goods.OverruleMsg = a.OverruleMsg
	return nil
}

/*
状态
*/
func (a *GoodsStatus) FillToStatus(goods *Goods) error {
	goods.Status = a.Status
	return nil
}

/*
完成订单操作 UpdatePrice
*/
func (a *GoodsPrice) FillToPrice(goods *Goods) error {
	goods.Price = a.Price
	return nil
}

/*
急售
*/
func (a *GoodsSale) FillToSale(goods *Goods) error {
	goods.Sale = a.Sale
	return nil
}

/*
精选
*/
func (a *GoodsSelection) FillToSelection(goods *Goods) error {
	goods.Selection = a.Selection
	return nil
}

/*
分配ID
*/
func (a *GoodsZhipai) FillToApproved(goods *Goods) error {
	goods.WID = a.WID
	return nil
}

/*
推荐首页
*/
func (a *GoodsRecommend) FillToRecommend(goods *Goods) error {
	goods.Recommend = a.Recommend
	return nil
}

// Convert `GoodsForm` to `Goods` object.
func (a *GoodsForm) FillTo(goods *Goods) error {
	goods.Status = "enabled"
	goods.Username = a.Username
	goods.Code = a.Code // 设备编号
	goods.Phone = a.Phone
	goods.Province = a.Province
	goods.ProvinceId = a.ProvinceId
	goods.City = a.City
	goods.CityId = a.CityId
	goods.District = a.District
	goods.DistrictId = a.DistrictId
	goods.BusID = a.BusID
	goods.Address = a.Address
	goods.Types = a.Types
	goods.TwoName = a.TwoName
	goods.TwoImage = a.TwoImage
	goods.AttrIds = a.AttrIds
	return nil
}
